//
//  UILable+Extensions.swift
//  Extensions
//
//  Created by gupengling on 2021/6/5.
//  Copyright © 2021 gupengling. All rights reserved.
//

import UIKit
public extension WDExtension where Base: UILabel {
    @discardableResult
    func number(_ ofLines: Int) -> WDExtension {
        base.numberOfLines = ofLines
        return self
    }
    @discardableResult
    func line(_ breakMode: NSLineBreakMode) -> WDExtension {
        base.lineBreakMode = breakMode
        return self
    }
    @discardableResult
    func baseline(_ adjustment: UIBaselineAdjustment) -> WDExtension {
        base.baselineAdjustment = adjustment
        return self
    }
    @discardableResult
    func allows(_ defaultTighteningForTruncation: Bool) -> WDExtension {
        base.allowsDefaultTighteningForTruncation = defaultTighteningForTruncation
        return self
    }
    @discardableResult
    func preferred(_ maxLayoutWidth: CGFloat) -> WDExtension {
        base.preferredMaxLayoutWidth = maxLayoutWidth
        return self
    }
    @discardableResult
    func adjusts(_ fontSizeToFitWidth: Bool) -> WDExtension {
        base.adjustsFontSizeToFitWidth = fontSizeToFitWidth
        return self
    }
    @discardableResult
    func highlighted(_ textColor: UIColor) -> WDExtension {
        base.highlightedTextColor = textColor
        return self
    }
    @discardableResult
    func minimum(_ scaleFactor: CGFloat) -> WDExtension {
        base.minimumScaleFactor = scaleFactor
        return self
    }
    @discardableResult
    func alignment(_ alignment: NSTextAlignment) -> WDExtension {
        base.textAlignment = alignment
        return self
    }
    /*
    @discardableResult
    func line(_ spacing:CGFloat) -> WDExtension {
        var textDic =
            [NSAttributedString.Key.foregroundColor:base.textColor,
             NSAttributedString.Key.font:base.font]
        let style = NSMutableParagraphStyle()
        style.lineSpacing = spacing
        textDic[NSAttributedString.Key.paragraphStyle] = style
        if base.text!.count > 0 {
            base.attributedText = NSAttributedString(string: base.text!, attributes: textDic)
        }
        else if base.attributedText!.length > 0 {
            var attStr =  NSMutableAttributedString(attributedString: base.attributedText ?? NSAttributedString(string: base.text!))
            attStr.setAttributes(textDic, range: NSMakeRange(0, base.attributedText!.length))
        }
        return self
    }*/
    
    
}
public extension UILabel {
    /// UILable遍历构造器
    ///
    /// - Parameters:
    ///   - title: 文字
    ///   - textColor: 文字颜色
    ///   - fontSize: 文字大小
    ///   - numOfLines: 文字行数
    ///   - alignment: 对齐方式
    convenience init(title: String?,
                     textColor: UIColor = UIColor.darkGray,
                     font: UIFont = UIFont.systemFont(ofSize: 14, weight: .regular),
                     numOfLines: Int = 0,
                     alignment: NSTextAlignment = .left){
        
        self.init()
        
        self.text = title
        self.textColor = textColor
        self.font = font//UIFont(name: "DINCond-Bold", size: fontSize)
        self.numberOfLines = numOfLines
        self.textAlignment = alignment
        
        self.sizeToFit()
    }
}
